<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class UserRegister extends CFormModel
{
	public $username;
	public $password;
	public $email;
	public $confirmpassword;

	private $_identity;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// username and password are required
			array('username, password,email,confirmpassword', 'required'),
			// password needs to be authenticated
			array('username','usercheck'),
			array('email','emailcheck'),
			array('confirmpassword','authenticate'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array();
	}

	/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	
	public function usercheck($attribute,$params){
		$user = Users::model()->find('username=:username',array(':username'=>$this->username));
		if($user != null)
			$this->addError('username','Username is exist!');
	}
	
	public function emailcheck($attribute,$params){
		$user = Users::model()->find('email=:email',array(':email'=>$this->email));
		if($user != null)
			$this->addError('email','This email has been registed!');
	}
	
	
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())
		{
			if($this->password != $this->confirmpassword)
				$this->addError('confirmpassword','The password you input twice is not the same');
		}
	}

	/**
	 * Logs in the user using the given username and password in the model.
	 * @return boolean whether login is successful
	 */
	public function regist()
	{
		/*if($this->_identity===null)
		{
			$this->_identity=new UserIdentity($this->username,$this->password);
			$this->_identity->authenticate();
		}
		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
		{
			$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
			Yii::app()->user->login($this->_identity,$duration);
			return true;
		}
		else
			return false;*/
	}
}
